package krt.wid.gslzs.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.yanzhenjie.nohttp.rest.Response;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import krt.wid.gslzs.adpater.RecyclerNewsAdpater;
import krt.wid.gslzs.bean.NewBean;
import krt.wid.gslzs.R;
import krt.wid.gslzs.base.BaseActivity;
import krt.wid.gslzs.util.UrlUtil;
import krt.wid.http.MHttp;
import krt.wid.http.MResponseListener;
import krt.wid.http.Result;
import krt.wid.util.MToast;
import krt.wid.gslzs.util.GlideImageLoade;
import krt.wid.gslzs.util.SpaceItemDecoration;
import krt.wid.gslzs.util.TitleManager;

/**
 * @author admin
 * @date 2018/5/8
 * 企业角色登录首页
 */

public class QyLoginMainActivity extends BaseActivity implements View.OnClickListener {
    ImageView sy_zqhd;
    Banner banner;
    RecyclerView recyclerView;
    //新闻adpater
    private RecyclerNewsAdpater recyclerNewsAdpater;
    private List<NewBean> datalist = new ArrayList<>(); //返回的查询结果

    private TwinklingRefreshLayout switf;
    String accesstoken;

    int yema=0;

    @Override
    public int bindLayout() {
        return R.layout.qysy_main;
    }

    @Override
    public void initView() {
        //设置主题栏
        TitleManager titleManager = new TitleManager(this);
        titleManager.hideDivider();
        titleManager.visableLeftImage(false);
        titleManager.setRightImage(R.mipmap.sy_icon1);
        titleManager.setCenterText("江西省政企直通平台", ContextCompat.getColor(this, R.color.white), 18);

        //政企互动
        sy_zqhd = findViewById(R.id.sy_zqhd);
        sy_zqhd.setOnClickListener(this);
        findViewById(R.id.sqpj_layout).setOnClickListener(this);
        findViewById(R.id.fgzx_layout).setOnClickListener(this);

        banner = (Banner) findViewById(R.id.banner);
        //本地图片数据（资源文件）
        List<Integer> list = new ArrayList<>();
        list.add(R.mipmap.banner1);
        list.add(R.mipmap.banner1);
        list.add(R.mipmap.banner1);
        banner.setImages(list)
                .setImageLoader(new GlideImageLoade())
                .start();

        //设置轮播图片点击事件
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (position == 0) {
                    MToast.showToast(QyLoginMainActivity.this, "点击了第一个页面");
                } else if (position == 1) {
                    MToast.showToast(QyLoginMainActivity.this, "点击了第二个页面");
                } else if (position == 2) {
                    MToast.showToast(QyLoginMainActivity.this, "点击了第三个页面");
                }

            }
        });
        SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
        accesstoken = sp.getString("accesstoken", null);

        //设置Recycler全类
        recyclerView = findViewById(R.id.new_recycler);
        //设置列表上下边距
        recyclerView.addItemDecoration(new SpaceItemDecoration(5));

        String url = "http://111.75.240.74:13381/non_public_economy/api/ImageDateList";
        MHttp.<Result<List<NewBean>>>create(this).setUrl(url)
                .addParams("accessToken", accesstoken)
                .addParams("start", yema+"")
                .addParams("length", "3")
                .execute(new MResponseListener<Result<List<NewBean>>>() {

                    @Override
                    public void onSuccess(int what, Result<List<NewBean>> result) {
                        if (result.isSuccess()) {
                            MToast.showToast(QyLoginMainActivity.this, result.getData().get(0).getContent());
                            datalist.clear();
                            datalist.addAll(result.getData());
                            recyclerNewsAdpater.notifyDataSetChanged();
                        }

                    }

                    @Override
                    public void onFailed(int what, Response<Result<List<NewBean>>> response) {

                    }
                });
        recyclerNewsAdpater = new RecyclerNewsAdpater(datalist); //推线上置为空
        recyclerNewsAdpater.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(QyLoginMainActivity.this, WebActivity.class).putExtra("url", UrlUtil.getUrl("FeiGongZiXunXiangQing") + "/" + datalist.get(position).getId()));
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(recyclerNewsAdpater);
        switf=findViewById(R.id.swipe);
        switf.setEnableRefresh(false);

        LoadingView loadingView = new LoadingView(this);
        switf.setBottomView(loadingView);
        switf.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                String url = "http://111.75.240.74:13381/non_public_economy/api/ImageDateList";
                MHttp.<Result<List<NewBean>>>create(QyLoginMainActivity.this).setUrl(url)
                        .addParams("accessToken", accesstoken)
                        .addParams("start", (yema+1)+"")
                        .addParams("length", "10")
                        .execute(new MResponseListener<Result<List<NewBean>>>() {
                            @Override
                            public void onSuccess(int what, Result<List<NewBean>> result) {
                                if (result.isSuccess()) {
                                    MToast.showToast(QyLoginMainActivity.this, result.getData().get(0).getContent());
                                    datalist.clear();
                                    datalist.addAll(result.getData());
                                    recyclerNewsAdpater.notifyDataSetChanged();
                                }

                            }

                            @Override
                            public void onFailed(int what, Response<Result<List<NewBean>>> response) {

                            }
                        });

            }
        });


    }

    @Override
    public void loadData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sy_zqhd:
                startActivity(new Intent(QyLoginMainActivity.this, AppealActivity.class));
                break;
            case R.id.sqpj_layout:
                startActivity(new Intent(QyLoginMainActivity.this, WebActivity.class).putExtra("url", UrlUtil.getUrl("SuQiuPingJia")));
                break;
            case R.id.fgzx_layout:
                startActivity(new Intent(QyLoginMainActivity.this, WebActivity.class).putExtra("url", UrlUtil.getUrl("FeiGongZiXun")));
                break;
            default:
                break;
        }

    }
}
